package com.starbaba.stat.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.starbaba.base.dao.LoanManagerBaseDao;
import com.starbaba.stat.service.StatOneKeyApiService;
import com.tuanzi.server.loan_base.model.db.stat.StatOneKeyApiOrder;

public class StatOneKeyApiServiceImpl extends LoanManagerBaseDao implements StatOneKeyApiService{
	
	
	@Override
	public List<StatOneKeyApiOrder> statExternalOneKeyApiOrder(
			Map<String, Object> paramsMap, int pageNo, int pageSize) {
		String sql = generateSql(paramsMap) + " ORDER BY cdate DESC LIMIT " + pageNo + "," + pageSize;
		List<StatOneKeyApiOrder> list = null;
		try {
			list = getStatDao().list(sql, StatOneKeyApiOrder.class);
		} catch (AppException e) {
			LogService.error(e);
		}
		return list;
	}
	
	
	@Override
	public int countExternalOneKeyApiOrder(Map<String, Object> paramsMap) {
		String getListSql = generateSql(paramsMap);
		String sql = " SELECT COUNT(1) FROM " + "(" + getListSql  + ") as a";
		int result = 0;
		try {
			result = getStatDao().getInt(sql);
		} catch (AppException e) {
			LogService.error(e);
		}
		return result;
	}
	
	
	public String generateSql(Map<String, Object> paramsMap){
		
		String startDate = (String) paramsMap.get("startDate");
		String endDate = (String) paramsMap.get("endDate");
		String channel = (String) paramsMap.get("channel");
		String platform = (String) paramsMap.get("platform");
		String status = (String)paramsMap.get("status");
		
		StringBuilder sql = new StringBuilder("SELECT * FROM `stat_onekey_api_order` WHERE prd_id = 8006 ");
		
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" and cdate >= '" + startDate + "'");
		}
		
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" and cdate <= '" + endDate + "'");
		}
		
		if(StringUtils.isNotEmpty(channel)){
			sql.append(" and channel = '" + channel + "'");
		}
		
		if(!platform.equals("ALL")){
			sql.append(" and platform = '" + platform + "'");
		}
		
		if(!status.equals("0")){
			sql.append(" and status = " + status);
		}
		
		return sql.toString();
		
	}

}
